<?php

class homeAction extends BaseAction {
    
    private function getSinaAppConn(){
        
        include_once(ROOT. '/cgi/sina/config.php' );
        include_once(ROOT. '/cgi/sina/weibooauth.php' );

        return  new WeiboClient( WB_AKEY , WB_SKEY ,  token_key, token_secret);
    }
    
    public function actionGo() {
        echo get_block('home', 'access');
    }

    public function actionIndex() {
        set_time_limit(60);
        if(array_key_exists('user', $_SESSION)){
            $this->redirect('user');
        }
        $this->view->layout = 'u22';

        $rs = Cookies::getMine('u22_url');
        //$rs = "'". str_replace(',', "','", $rs) ."'";
        $rs = explode(',', $rs);
        $cb = array();
        $w  = null;
        if(count($rs) > 0) {
            for($i=0;$i<count($rs);$i++) {
                $cb[]   = '?';
            }
            $w  = implode(',', $cb);
        }
        $sql    = "select * from {#prefix#}url where uhash in (". $w.") order by id desc";
        $re = DAO_Finder::result($sql, DB_URL, $rs);
        if(empty($re)) {
            Cookies::remove('u22_url');
        }
        $this->set('rs', $re);
        $this->render();
    }
    
    public function actionIndexNew() {
        if(array_key_exists('user', $_SESSION)){
            $this->redirect('user');
        }
        $this->view->layout = 'u22_new';
        $this->render();
    }

    /**
     * 腾讯微博登陆
     */
    public function actionTencentLogin(){
        
        if(empty($_SESSION['weibo']['tencent'])) {
            exit('非法访问');
        }
        unset ($_SESSION['app_key_tencent']);
        
        $tencent    = $_SESSION['weibo']['tencent']['data'];
        $email      = $tencent['email'];
        $email   = $tencent['name'].'@qq';
        $has    = DB_USER::hasEmail($email);
        //未注册
        if(!$has){
            $pass   = Common::getPassword('123');
            $user   = new DB_USER();
            $user->setEmail($email);
            $user->setLevel(1);
            $user->setPassword($pass);
            $user->setIsActive(0);
            $user->save();
            $_SESSION['user']   = $user->getArray();
        }else{
            $_SESSION['user']   = $has->getArray();
        }
        //保存appkey
        if(!empty($_SESSION['last_key_tencent'])){
            $key    = $_SESSION['last_key_tencent'];
            DB_APP_KEY_TENCENT::setUpdate($key['oauth_token'], $key['oauth_token_secret'],$_SESSION['user']['id'], $tencent['name']);
        }

        get_block('home', 'set_url_uid');
        $this->redirect('user/');
    }

    /**
     * 新浪登陆，重置密码
     */
    public function actionSinaLogin(){
        if(!array_key_exists('last_key_sina', $_SESSION)){
            exit('非法访问');
        }
        //$has    = DB_APP_KEY_SINA::setUpdate($_SESSION['last_key_sina']['oauth_token'], $_SESSION['last_key_sina']['oauth_token_secret'], 0, $_SESSION['last_key_sina']['user_id']);
        
        
        $email  = $_SESSION['last_key_sina']['user_id'];
        $email   = $_SESSION['last_key_sina']['me']['screen_name'];
        $has    = DB_USER::hasEmail($email);
        if(!$has){
            $pass   = Common::getPassword('123');
            $has   = new DB_USER();
            $has->setEmail($email);
            $has->setLevel(1);
            $has->setPassword($pass);
            $has->setIsActive(0);
            $has->save();
            $_SESSION['user']   = $has->getArray();
        }else{
            $_SESSION['user']   = $has->getArray();
         }
        //保存appkey
        if(!empty($_SESSION['last_key_sina'])){
            $key    = $_SESSION['last_key_sina'];
            DB_APP_KEY_SINA::setUpdate($key['oauth_token'], $key['oauth_token_secret'],$has->getId(), $has->getEmail());
        }
        get_block('home', 'set_url_uid');
        
       $this->redirect('user/index');
    }

    /**
     * 重置密码
     */
    public function actionResetPassword() {

        $password   = $this->postVar('password');
        $email   = $this->postVar('email');
        $password   = Common::getPassword($password);

        if($email && $password) {
            $user   = new DB_USER();
            $user->setEmail($email);
            $user->setPassword($password);
            $user->save();
            $user   = $user->getArray();
            $this->setSession('user', $user);

            if(array_key_exists('sina_id', $_SESSION)) {
                $sina   = DAO_Finder::findById(DB_APP_KEY_SINA, $_SESSION['sina_id']);
                $sina->setUid($_SESSION['sina_id']);
                $sina->save();
                unset($_SESSION['sina_id']);
            }    
            echo 'ok';
            exit;
        }
        echo 'error';
    }

    public function actionNotFound() {
        echo 'the page not found!';
    }
    
    function actionShowShareStatusesWeibo(){
        $this->setPageTitle('查询指定单个短链接的最新微博内容');
        $this->view->layout = '';
        $url    = trim($this->params['url']);
        $u    = 'http://t.cn/';
        
        if(empty($url)) {
            $u    .= 'hIXSg';
            $this->render('show_share_statuses_weibo_index');
            exit;
        }else{
            $u      .=$url;
        }
        
        $re = array();
        
        if(!empty($url)) {
            $c      = $this->getSinaAppConn();
            $parameters = array('count'=>'20');
            $parameters = array('page'=>'1','count'=>200);
            if(isset($_SESSION['id'])){
                //$parameters['since_id'] = $_SESSION['id'];
            }
            $re     = $c->short_url_share_statuses($u, $parameters);

        }
        $this->set('url', $url);
        $this->set('rs', $re);
        $this->render();
    }
    
    function actionShowShareStatusesComment(){
        $this->setPageTitle('查询指定单个短链接的最新微博评论');
        $url    = $this->getVar('url');
        $u    = 'http://t.cn/';
        if(empty($url)) {
            $u    .= 'hIXSg';
        }else{
            $u      .=$url;
        }
        
        $re = array();
        if(!empty($url)) {
            $c      = $this->getSinaAppConn();
            $parameters = array('count'=>'200');
            if(isset($_SESSION['id'])){
                //$parameters['since_id'] = $_SESSION['id'];
            }
            $re     = $c->short_url_comment_comments($u,$parameters);
        }
        $this->set('rs', $re);
        $this->render();
    }
    
    function actionShowWeiboComment(){
        $id = $this->params['id'];
        $c  = $this->getSinaAppConn();
        $re = $c->get_comments_by_sid($id, 1, 20);
        print_r($re);
    }
    
    function actionTest(){
        $c  = $this->getSinaAppConn();
        $re = $c->friendships_friends_bilateral(array('count'=>'200','uid'=>'2350462347'));
        print_r($re);
    }
}

